package com.xujieyang.study.thrift;

import org.apache.thrift.TProcessor;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

public class Main {
	public static void main(String[] args) {
		startServerSimple();
	}

	public static void startServerSimple() {
		System.out.println("HelloWorld TSimpleServer start ....");
		TProcessor tProcessor = 
//				new HelloWorldService.AsyncProcessor<HelloWorldService.AsyncIface>(new HelloWorldImpl2());
				new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
		
		// 简单的单线程服务模型，一般用于测试
		TServerSocket serverTransport;
		try {
			serverTransport = new TServerSocket(6000);
			TServer.Args tArgs = new TServer.Args(serverTransport);
			tArgs.processor(tProcessor);
			tArgs.protocolFactory(new TBinaryProtocol.Factory());
			TServer server = new TSimpleServer(tArgs);
			System.out.println("111111111111111");
			server.serve();
		} catch (TTransportException e) {
			System.out.println("Server start error!!!");
			e.printStackTrace();
		}
	}

	public static void startServerThreadPool() {
		System.out.println("HelloWorld TThreadPoolServer start ....");
		TProcessor tProcessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
		TServerSocket serverTransport;
		try {
			serverTransport = new TServerSocket(60000);
			TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);
			ttpsArgs.processor(tProcessor);
			ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());
			// 线程池服务模型，使用标准的阻塞式IO，预先创建一组线程处理请求。
			TServer server = new TThreadPoolServer(ttpsArgs);
			System.out.println("111111111111111");
			server.serve();
		} catch (TTransportException e) {
			e.printStackTrace();
		}
	}
	
	public static void startServerNonblocking() {
		try {
			System.out.println("HelloWorld TNonblockingServer start ....");
			TProcessor tProcessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
//			TProcessor tProcessor = new HelloWorldService.AsyncProcessor<HelloWorldService.AsyncIface>(new HelloWorldImpl2());
			TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(60000);
			TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);
			tnbArgs.processor(tProcessor);
			tnbArgs.transportFactory(new TFramedTransport.Factory());
			tnbArgs.protocolFactory(new TCompactProtocol.Factory());
			// 使用非阻塞式IO，服务端和客户端需要指定TFramedTransport数据传输的方式
			TServer server = new TNonblockingServer(tnbArgs);
			System.out.println("111111111111111");
			server.serve();
 
		} catch (TTransportException e) {
			System.out.println("Server start error!!!");
			e.printStackTrace();
		}
	}
	
	public static void startServerHahs() {
		try {
			System.out.println("HelloWorld THsHaServer start ....");
			TProcessor tProcessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
			TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(6000);
			THsHaServer.Args hahsArgs = new THsHaServer.Args(tnbSocketTransport);
			hahsArgs.protocolFactory(new TCompactProtocol.Factory());  
			hahsArgs.transportFactory(new TFramedTransport.Factory());  
			hahsArgs.processorFactory(new TProcessorFactory(tProcessor));  
            TServer server = new THsHaServer(hahsArgs);  
            server.serve();  
		} catch (Exception e) {
			System.out.println("Server start error!!!");
			e.printStackTrace();
		}
	}
}
